package com.caiheng.api.service.platfom.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.estate.PropertyMapper;
import com.caiheng.api.dao.mer.MerMapper;
import com.caiheng.api.dao.platform.SplitAccountMapper;
import com.caiheng.api.dao.platform.SplitAccountRecordMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.Estate;
import com.caiheng.api.entity.estate.Property;
import com.caiheng.api.entity.mer.Mer;
import com.caiheng.api.entity.platform.SplitAccount;
import com.caiheng.api.entity.platform.SplitAccountRecord;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.platfom.SplitAccountRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分成账户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-08
 */
@Service
public class SplitAccountRecordServiceImpl extends BaseServiceImpl<SplitAccountRecordMapper, SplitAccountRecord> implements SplitAccountRecordService {

    @Resource
    private SplitAccountRecordMapper accountRecordMapper;
    @Resource
    private SplitAccountMapper accountMapper;
    @Resource
    private PropertyMapper propertyMapper;
    @Resource
    private EstateMapper estateMapper;
    @Resource
    private MerMapper merMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<SplitAccountRecord> getPageByUserId(int userId,int account_id,Integer from_type,int pageIndex,int pageSize) {

        QueryWrapper<SplitAccountRecord> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        if(from_type!=null){
            qw.eq("from_type",from_type);
        }
        if(account_id > 0){
            qw.eq("account_id",account_id);
        }
        qw.orderByDesc("create_time");
        List<SplitAccountRecord> list = accountRecordMapper.getPageEntityList(qw,pageIndex,pageSize);
        if(CollUtil.isNotEmpty(list)){
            for (SplitAccountRecord accountRecord : list){
                //动账类型  0 - 提现 1 - 重置  2 - 分成增加
                SplitAccount splitAccount = accountMapper.selectById(accountRecord.getAccountId());
                if(splitAccount!=null){
                    //账户类型   0 - 平台   1 - 物业  2 -  小区  3 - 商家   4 - 个人
                    if(splitAccount.getAccountType() == 0){

                    }else if(splitAccount.getAccountType() == 1){
                        Property property = propertyMapper.selectById(splitAccount.getAccountId());
                        if(property!=null){
                            accountRecord.setAccountName(property.getPropertyName());
                        }
                    }else if(splitAccount.getAccountType() == 2){
                        Estate estate = estateMapper.selectById(splitAccount.getAccountId());
                        if(estate!=null){
                            accountRecord.setAccountName(estate.getEstateName()+"(物业账户)");
                        }
                    }else if(splitAccount.getAccountType() == 3){
                        Mer mer = merMapper.selectById(splitAccount.getAccountId());
                        if(mer!=null){
                            accountRecord.setAccountName(mer.getShopName()+ "(商家账户)");
                        }
                    }else if(splitAccount.getAccountType() == 4){
                        User user = userMapper.selectById(splitAccount.getAccountId());
                        if(user!=null){
                            accountRecord.setAccountName(user.getNickName() + "(个人账户)");
                        }
                    }
                }
            }
        }
        return list;
    }
}
